const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');
const App = getApp();

Page({
  data: {
    defaultimg: 'http://oss-img.meitianhui.com/test/communityDocs/201801/51a75f1fc3f445cba238b145faff1d41.jpg',
    modalHidden: true,
    stores_id:"",
    goodsType:'',
    goods_id: '',
    //wxml渲染层参数
    rps_data: {
      list: [],
      page: {
        page_no: 0,
        page_size: 10,
      },
      paginate: {}
    },
    //当前详细地址
    CurrentDetailAddress: ''
  },

  //初始化数据加载
  onLoad: function (option){
    this.setData({
      goodsType: option.goodsType,
      goods_id: option.goods_id,
      CurrentDetailAddress: App.globalData.CurrentDetailAddress
    })
    this.getList();
  },

  getList() {
    const self = this;

    let pageData = {
      page_no: this.data.rps_data.page.page_no + 1,
      page_size: this.data.rps_data.page.page_size,
    }

    let data = {
      service: 'stores.consumer.nearbyStoresTypeListPageFind',
      page: JSON.stringify(pageData),
      params: '{"range":"1000","latitude":"","longitude":""}',
      url_type: 'member',
      app_token: App.globalData.app_token,
      security_code: App.globalData.security_code 
    }
    
    var jsons = JSON.parse(data.params);
    jsons["latitude"] = App.globalData.locationInfo.latitude,
    jsons["longitude"] = App.globalData.locationInfo.longitude,
    data.params = JSON.stringify(jsons);


    var rps_data = this.data.rps_data

    api.reqData({
      data,
      success: (res) => {
        console.log(res.data.data)
        
        //处理过滤数据，根据
        var json = res.data.data.list;
        var doc_url = res.data.data.doc_url;
        var doc_url_key = Object.keys(doc_url)
        for (var i = 0; i < json.length; i++) {
          //门店头像图片展示  doc_url和里面头像分离
          var neighbor_pic_path_str = json[i].neighbor_pic_path;
          if (neighbor_pic_path_str != '') {
            for (var e = 0; e < doc_url_key.length; e++) {
              if (neighbor_pic_path_str == doc_url_key[e]) {
                json[i].neighbor_pic_path = doc_url[doc_url_key[e]].replace("https", "http")
              }
            }
          } else {
            json[i].pic_info = ''
          }

          //adress过长得情况在前台处理
          let address = json[i].address;
          if (address.indexOf("/") != -1) {
            let start = address.lastIndexOf('/') + 1;
            json[i].address = address.substring(start);
          }
        }


        rps_data.list = [...rps_data.list, ...json]
        rps_data.page.page_no = res.data.data.page.page_no
        rps_data.page.page_size = res.data.data.page.page_size
        //paginate.total_page = res.data.data.page.total_page
        rps_data.paginate.total_count = res.data.data.page.total_count
        //判断hasNext
        if (rps_data.paginate.total_count > rps_data.page.page_no * rps_data.page.page_size) {
          console.log("后面有数据 还没有查询完")
          rps_data.paginate.hasNext = true
        } else {
          console.log("后面没有数据了，不要再拉了")
          rps_data.paginate.hasNext = false
        }
        this.setData({
          rps_data: rps_data,
        })
      },
    })
  },

  //拉到底部刷新
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    if (!this.data.rps_data.paginate.hasNext) return
    this.getList()
  },


  //页面弹出半透明窗口js
  modalTap: function (e) {
    var stores_id = e.currentTarget.dataset.id;
    this.setData({
      modalHidden: false,
      stores_id: stores_id
    })
  },

  modalChange: function (e) {
    //确认按钮调用 默认门店接口
    const self = this;
    let data = {
      service: 'stores.consumer.favoriteStore',
      params: '{"consumer_id":"11432431","is_llm_stores":"Y","stores_id":"cacabbe3-da98-11e5-a165-00163e010763"}',
      url_type: 'member',
      app_token: App.globalData.app_token,
      security_code: App.globalData.security_code 
    }

    var json = JSON.parse(data.params);
    //淘淘领传值
    var stores_id = e.currentTarget.dataset.id;
    json["stores_id"] = e.currentTarget.dataset.id;
    json["consumer_id"] = App.globalData.memberInfo.member_id;
    data.params = JSON.stringify(json);

    //let sign = util.getSign(data);
    //data.sign = sign;
    api.reqData({
      data,
      success: (res) => {
        //这个得写法和格式 setData({........})
        console.log(res.data+"---------------------------");
        let goodsType = this.data.goodsType
        let goods_id = this.data.goods_id

        if (res.data.rsp_code === "succ") {
          //避免后退是还出现弹出框，先隐藏弹出框在跳转
          this.setData({
            modalHidden: true
          })
          
          if (goodsType == 1) {
            wx.navigateTo({
              url: '../detail/taotaoling/taotaoling?goods_id=' + goods_id + '&stores_id=' + stores_id
            })
          } else {
            wx.navigateTo({
              url: '../submitOrder/submitOrder?goods_id=' + goods_id
            })
          }
        } else {
          this.setData({
            modalHidden: true
          })
        }
      },
    })
  },
  
  modalChange1: function (e) {
    this.setData({
      modalHidden: true
    })
  },

})
